import { defineStore } from "pinia";
import { SessionStorage } from "@webxue/common";
import type { WebxRouteTab } from "../hooks";
import useMapping from "../utils/mapping.util";

export const webxRouteTabsStore = defineStore("webxRouteTabs", {
  state:  () => ({
    // 路由刷新的key
    routeKey:Date.now(),
    // 当前所有的页签
    dataSource:(SessionStorage.get(useMapping().SessionKeys.routeTabs)?.dataSource || []) as WebxRouteTab[]
  }),
  actions:{
    // 刷新页签
    setRouteKey(){
      this.routeKey = Date.now();
    },
    // 设置所有页签
    setDataSource(dataSource:WebxRouteTab[]){
      this.dataSource = dataSource;
      const routeTabsConfig = SessionStorage.get(useMapping().SessionKeys.routeTabs) || {};
      routeTabsConfig.dataSource = dataSource;
      SessionStorage.set(useMapping().SessionKeys.routeTabs, routeTabsConfig);
    }
  }
})